/*
 * @Author: your name
 * @Date: 2020-11-30 08:52:47
 * @LastEditTime: 2020-12-01 06:40:07
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /fast-fasta2kmers/src/gen_tokens.cpp
 */
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#include <string.h>

#include<iostream>
using namespace std;

void ordTo4(int n,int m,int re[])
{
    // 十进制数转m进制数
    int ind =0;
    while(n)
    {
        re[ind++] = n%m;
        n = n/m; 
    }
    // int tmp;
    // for (int pos = 0;ind-pos>pos;pos++){
    //     tmp = re[pos];
    //     re[pos] = re[ind-pos];
    //     re[ind-pos] = tmp;
    // }

}

void permutation(FILE *fw,int k,int m)
{
    int *codes =(int*)malloc(k*sizeof(int));
    char *tmp =(char*)malloc((k+1)*sizeof(char));
    // FILE *f= fopen(fname,"w");
    tmp[k] = 0;
    int total= pow(m,k);
    for (int num = 0;num< total;num++)
    {
        ordTo4(num,m,codes);
        
        for (int i = 0;i<k;i++)
        {

            if(codes[i] == 0)
            {
                tmp[i] = 'A';
            }
            else if (codes[i] == 1)
            {
                tmp[i] = 'G';
            }
            else if (codes[i] == 2)
            {
                tmp[i] = 'C';
            }
            else if (codes[i] == 3)
            {
                tmp[i] = 'T';
            }
        }
        fprintf(fw,"%s\n",tmp);
    }
    
}


int main(int argc,char *argv[]){
    int k = atoi(argv[1]);
    int m = atoi(argv[2]);
    
    const char *fname = "tokens-9mer.txt";
    FILE *fw = fopen(fname,"w");

    if (fw ==nullptr){
        fprintf(stderr,"file cannot opened!");
        
    }
    else
    {
        permutation(fw,k,m);
        fclose(fw);
    }

    
    return 0;
}


